<?php
if($this->session->userdata('role') != null){
	if($this->session->userdata('role') == 1){
include 'include/header.php';
?>
<script>
</script>
<div class="form-group">
<?php
if(!empty($data_responden))
{
?>
<div>
	<button class="btn btn-primary" onclick="test()" id="tombol_uji" disabled>Uji Data</button>
	<!--button class="btn btn-success" onclick="modal_tanda_reliabel()">Beri Tanda Data Reliabel</button>
	<button class="btn btn-warning" onclick="modal_tanda_valid()">Beri Tanda Data Valid</button-->
</div>
<?php
}
?>
<input id="id_kuesioner" value="<?php echo $id_kuesioner;?>" hidden>
<h4>Data Responden</h4>
<div style="overflow: scroll; width: 1347px; height: 520px;">
				<table class="table" id="data">
				<?php
				if(!empty($data_responden))
				{
					$nomor = 0;
					echo '<tr >';
					echo '<th>No.</th>';
					for($a = 0; $a < count($pertanyaan); $a++)
					{
						echo '<th class="kolom" id="'.$pertanyaan[$a]['fk_id_jenis_pertanyaan'].'">'.$pertanyaan[$a]['pertanyaan'].'</th>';	
					}
					echo '<th>Aksi</th>';
					echo '</tr>';
					//var_dump($pertanyaan['pertanyaan']);
					foreach($data_responden as $responden)
					{
					$nomor++;
					echo '<tr id="row-'.$nomor.'">';
					echo '<td style="color: black;">'.$nomor.'</td>';
						for($i = 0; $i < count($pertanyaan); $i++)
						{
							echo '<td class="'.$i.'">'.$responden['data_jawaban'][$i]['jawaban'].'</td>';
							$id_resp = $responden['data_jawaban'][$i]['fk_id_responden'];
						}
					echo '<td><button class="btn btn-danger" onclick="delete_responden('.$id_resp.','.$nomor.',event)" title="Hapus data responden?">X</button></td>';
					echo '</tr>';
					}
				} else
					{
						echo 'belum ada data responden';
					}
				?>
				</table>
</div>
</div>

<div class="modal fade" id="ajaxModal" style="margin-top:50px">
	<div class="modal-body">
		<div class="modal-dialog" style="width: 1275px;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">Uji Data</h4>
				</div>
				
				<div style="height:200px">
					<p style="margin-left: 260px;">Pilih variabel</p>
					<div class="modal-body col-lg-6" id="kolom_variabel" style="overflow: scroll; height: 100%;">
					
						
					</div>
					<p style="position: absolute; margin-left: 849px; margin-top: -29px;">Variabel yang diuji</p>	
					<div class="modal-body col-lg-6" id="kolom_ujidata" style="overflow: scroll; height: 100%;">
					
					</div>
				</div>
				<div class="modal-body" style="margin-top: 20px;">
				<label>Jenis pengujian</label>
				<select id="jenis_uji">
					<option value="0">Reliabilitas</option>
					<option value="1">Validitas</option>
					<option value="2">Normalitas</option>
				</select>
				</div>
				<div class="modal-footer">
					<button type="button" id="btn_uji" class="btn btn-primary" onclick="uji()" disabled>Uji</button>
					<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="modal fade" id="modal_validitas">
	<div class="modal-body">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title">Uji Validitas Data</h4>
				</div>
				<div class="modal-body" id="validitas" style="overflow: scroll;">
					
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-warning" onclick="show_rincian()">Lihat Detail</button>
					<input type="button" class="btn btn-primary" id="btndownload1" value="Simpan">
					<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="modal fade" id="modal_rincian_validitas">
	<div class="modal-body">
		<div class="modal-dialog" style="width: 1275px;">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title">Rincian Validitas Data</h4>
				</div>
				<label>Tabel Korelasi Per Item</label>
				<div class="modal-body" id="rincian_validitas" style="overflow: scroll;">
					
				</div>
				<div class="modal-footer">
					<input type="button" class="btn btn-primary" id="btndownload2" value="Simpan">
					<a class="btn btn-danger" data-dismiss="modal">Close</a>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="modal fade" id="modal_normalitas">
	<div class="modal-body">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title">Uji Normalitas Data</h4>
				</div>
				<div class="modal-body" id="normalitas" style="overflow: scroll;">
					
				</div>
				<div class="modal-footer">
					<input type="button" class="btn btn-primary" id="btndownload3" value="Simpan">
					<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="modal fade" id="modal_tanda_reliabel">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
		<div class="modal-body">
			Apakah Data sudah Reliabel?
		</div>
		<div class="modal-footer">
			<a class="btn btn-primary" onclick="tanda_reliabel(1)">Ya</a>
			<a class="btn btn-warning" onclick="tanda_reliabel(0)">Tidak</a>
			<a class="btn btn-danger" data-dismiss="modal">Close</a>
		</div>
    </div>
  </div>
</div>
<div class="modal fade" id="modal_tanda_valid">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
		<div class="modal-body">
			Apakah Data sudah Valid?
		</div>
		<div class="modal-footer">
			<a class="btn btn-primary" onclick="tanda_valid(1)">Ya</a>
			<a class="btn btn-warning" onclick="tanda_valid(0)">Tidak</a>
			<a class="btn btn-danger" data-dismiss="modal">Close</a>
		</div>
    </div>
  </div>
</div>
		
		
<script>

</script>
<script>
$("#btndownload1").click(function(e) {
		//getting values of current time for generating the file name
		var nama_data = 'hasil_uji_validitas';
		var dt = new Date();
		var day = dt.getDate();
		var month = dt.getMonth() + 1;
		var year = dt.getFullYear();
		var hour = dt.getHours();
		var mins = dt.getMinutes();
		var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
		//creating a temporary HTML link element (they support setting file names)
		var a = document.createElement('a');
		//getting data from our div that contains the HTML table
		var data_type = 'data:application/vnd.ms-excel';
		var table_div = document.getElementById('validitas');
		var table_html = table_div.outerHTML.replace(/ /g, '%20');
		a.href = data_type + ', ' + table_html;
		//setting the file name
		a.download = nama_data+'.xls';
		//triggering the function
		a.click();
		//just in case, prevent default behaviour
		e.preventDefault();
	});
	
$("#btndownload2").click(function(e) {
		//getting values of current time for generating the file name
		var nama_data = 'hasil_uji_validitas';
		var dt = new Date();
		var day = dt.getDate();
		var month = dt.getMonth() + 1;
		var year = dt.getFullYear();
		var hour = dt.getHours();
		var mins = dt.getMinutes();
		var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
		//creating a temporary HTML link element (they support setting file names)
		var a = document.createElement('a');
		//getting data from our div that contains the HTML table
		var data_type = 'data:application/vnd.ms-excel';
		var table_div = document.getElementById('rincian_validitas');
		var table_html = table_div.outerHTML.replace(/ /g, '%20');
		a.href = data_type + ', ' + table_html;
		//setting the file name
		a.download = nama_data+'.xls';
		//triggering the function
		a.click();
		//just in case, prevent default behaviour
		e.preventDefault();
	});

$("#btndownload3").click(function(e) {
		//getting values of current time for generating the file name
		var nama_data = 'hasil_uji_normalitas';
		var dt = new Date();
		var day = dt.getDate();
		var month = dt.getMonth() + 1;
		var year = dt.getFullYear();
		var hour = dt.getHours();
		var mins = dt.getMinutes();
		var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
		//creating a temporary HTML link element (they support setting file names)
		var a = document.createElement('a');
		//getting data from our div that contains the HTML table
		var data_type = 'data:application/vnd.ms-excel';
		var table_div = document.getElementById('normalitas');
		var table_html = table_div.outerHTML.replace(/ /g, '%20');
		a.href = data_type + ', ' + table_html;
		//setting the file name
		a.download = nama_data+'.xls';
		//triggering the function
		a.click();
		//just in case, prevent default behaviour
		e.preventDefault();
	});
	
$(document).ready(function(){
	<?php echo (count($data_responden) > 1)? "$('#tombol_uji').removeAttr('disabled');" : '' ?>
	/*$('#modal_validitas').on('hidden.bs.modal', function (e) {
		$('#modal_tanda_valid').modal('toggle');
	});
	$('#modal_rincian_validitas').on('hidden.bs.modal', function (e) {
		//$('#modal_tanda_valid').modal('toggle');
	});*/
})

var i;
var a = [];
var d = [];



function show_rincian()
{
	//$('#modal_validitas').modal('hide');
	$('#modal_rincian_validitas').modal('toggle');
} 

/*function close_validitas()
	{
		$('#modal_validitas').hide();
	}

function close_rincian_validitas()
	{
		$('#modal_rincian_validitas').hide();
	}*/

function delete_responden(id_resp,id,e)
	{
	
	var x = confirm('Anda yakin akan menghapus data ini?');
	if(x){
		$.post('<?php echo base_url(); ?>index.php/uji/delete_responden/', {
				id_responden: id_resp
			});
			$('#row-'+id).remove();
		} else{e.preventDefault();}
	
	} 

function test()
	{
		var b = 0;
		var c = 0;
		$('#btn_uji').attr('disabled', 'disabled');
		
		$('.kolom').each(function(){
			a[b++] = $(this).text();
			d[c++] = $(this).attr('id');
		});
				
		//console.log(d);
		var html='';
		$('.var_uji').remove();	
		for (i = 0; i < a.length; i++) { 
		$('#variabel-'+i).remove();			
		html += '<div>';
		if(d[i] == '1' || d[i] == '2'){
		html += '<button hidden>'+a[i]+'</button>';
		} else{
			html += '<button id="variabel-'+i+'" onclick="insert_var('+i+')" style="text-overflow: ellipsis; white-space: nowrap; overflow: hidden; width: 100px;">'+a[i]+'</button>';
		}
		html += '</div>';
		
		}
		
		$('#kolom_variabel').append(html);
		$('#ajaxModal').modal('show');
	}

function insert_var(variabel)
{
	var html = '<div>';
	html += '<button id="var_uji-'+variabel+'" class="var_uji" onclick="delete_var_uji('+variabel+')" style="text-overflow: ellipsis; white-space: nowrap; overflow: hidden; width: 100px;">'+a[variabel]+'</button>';
	html += '</div>';
	$('#variabel-'+variabel).css('display', 'none');
	$('#kolom_ujidata').append(html);
	if($('.var_uji').length > 1){
		$('#btn_uji').removeAttr('disabled');
	}
}

function delete_var_uji(variabel){
	$('#variabel-'+variabel).css('display', 'block');
	$('#var_uji-'+variabel).remove();
	if($('.var_uji').length <= 1){
		$('#btn_uji').attr('disabled', 'disabled');
	}
}

function uji()
{
	uji_loading();
	if($('#jenis_uji').val() == '0')
	{
	
		var c = 0;
		var var_uji = [];
		//var non_integer = 'false';
		$('.var_uji').each(function(){
			var_uji[c++] = $(this).prop('id').replace( /[^\d.]/g, '' );
		});
		//console.log(var_uji.length);
		var data = new Array(var_uji.length);
		for(var d = 0; d < var_uji.length; d++){
			//console.log(var_uji[d]);
			var e = 0;
			data[d] = new Array($('.'+var_uji[d]).length);
			$('.'+var_uji[d]).each(function(){
				data[d][e++] = parseInt($(this).text());
			});
		}
		
			$.ajax({
				url: '<?php echo base_url(); ?>index.php/uji/reliabilitas/',
				data: {'data' : data, 'id_kuesioner': $('#id_kuesioner').val()},
				type: 'post',
				success: function(data) {
									alert(data);
									hide_loading();
									//$('#modal_tanda_reliabel').modal('toggle');
									
									/*var reliabel = confirm('apakah data reliabel?', 'Ya', 'Tidak');
									if(!reliabel){return false;
									} else{
										$.post('<?php echo base_url(); ?>index.php/uji/tanda_reliabel/', {
											id_kuesioner: $('#id_kuesioner').val()
										});
										}*/
								}
			});
		
		
	} else {
		if($('#jenis_uji').val() == '1')
		{
			$('#rincian_validitas').empty()
			$('#validitas').empty()
			var c = 0;
			var var_uji = [];
			//var non_integer = 'false';
			$('.var_uji').each(function(){
				var_uji[c++] = $(this).prop('id').replace( /[^\d.]/g, '' );
			});
			//console.log(var_uji.length);
			var data = new Array(var_uji.length);
			for(var d = 0; d < var_uji.length; d++){
				//console.log(var_uji[d]);
				var e = 0;
				data[d] = new Array($('.'+var_uji[d]).length);
				$('.'+var_uji[d]).each(function(){
					data[d][e++] = parseInt($(this).text());
				});
			}
			
			$.post('<?php echo base_url(); ?>index.php/uji/validitas/', {
			data: data,
			id_kuesioner: $('#id_kuesioner').val()
			}, function(data){
				//console.log(data)
				var html = '<table class="table">';
				html += '<td></td>';
				var g = 0;
				for(var f = 0; f < data['validitas_nilai_total'].length; f++){
					g = f+1;
					html += '<td>Item'+ g+'</td>';
				}
				
				html += '<tr>';
				html += '<td>Total</td>';
				$.each(data['validitas_nilai_total'], function(key, value) {
					html += '<td>'+value+'</td>';
				});
				html += '</tr>';
				
				html += '<tr>';
				html += '<td>Keterangan</td>';
				$.each(data['validitas_nilai_total'], function(key1, value1) {
					if(value1 > data['tabel_r']){
						html += '<td>valid</td>';
					} else{
						html += '<td>tidak valid</td>';
					}
				});
				html += '</tr>';
				html += '<tr><td>Nilai Tabel R</td><td>'+data['tabel_r']+'</td></tr>';
				
				html += '</table>';
				
				var html2 = '<table class="table">';
				html2 += '<td></td>';
				var h = 0;
				var item = [];
				for(var i = 0; i < data['validitas_nilai_total'].length; i++){
					h = i+1;
					html2 += '<td>Item'+ h+'</td>';
					item[i] = '<td>Item'+ h+'</td>';
				}
				
				$.each(data['korelasi_tiap_variabel'], function(key2, value2) {
					html2 += '<tr>';
					html2 += item[key2];
					$.each(value2, function(key3, value3) {
						html2 += '<td>'+value3+'</td>';
					});
					html2 += '</tr>';
				});
				
				html2 += '<tr>';
				html2 += '<td style="font-weight: bold;">Total</td>';
				$.each(data['validitas_nilai_total'], function(key4, value4) {
					html2 += '<td style="font-weight: bold;">'+value4+'</td>';
				});
				html2 += '</tr>';
				
				html2 += '<tr>';
				html2 += '<td style="font-weight: bold;">Keterangan</td>';
				$.each(data['validitas_nilai_total'], function(key5, value5) {
					if(value5 > data['tabel_r']){
						html2 += '<td style="font-weight: bold;">valid</td>';
					} else{
						html2 += '<td>tidak valid</td>';
					}
				});
				html2 += '</tr>';
				
				html2 += '</table>';
				
				$('#rincian_validitas').append(html2);
				$('#validitas').append(html);
				//$('#ajaxModal').modal('hide');				
				$('#modal_validitas').modal('toggle');
				hide_loading();
				
			}, 'json');
			
		} else 
			{
				$('#normalitas').empty();
				var c = 0;
				var var_uji = [];
				//var non_integer = 'false';
				$('.var_uji').each(function(){
					var_uji[c++] = $(this).prop('id').replace( /[^\d.]/g, '' );
				});
				//console.log(var_uji.length);
				var data = new Array(var_uji.length);
				for(var d = 0; d < var_uji.length; d++){
					//console.log(var_uji[d]);
					var e = 0;
					data[d] = new Array($('.'+var_uji[d]).length);
					$('.'+var_uji[d]).each(function(){
						data[d][e++] = parseInt($(this).text());
					});
				}
				
				$.post('<?php echo base_url(); ?>index.php/uji/normalitas/', {
				data: data
				}, function(data){
					//alert(data);
					var html = '<table class="table">';
					html += '<td></td>';
					var g = 0;
					for(var f = 0; f < data['nilai_L_hitung'].length; f++)
					{
						g = f+1;
						html += '<td>Item'+ g+'</td>';
					}
					
					html += '<tr>';
					html += '<td>Hasil Uji</td>';
					$.each(data['nilai_L_hitung'], function(key, value) {
						html += '<td>'+value+'</td>';
					});
					html += '</tr>';
					
					html += '<tr>';
					html += '<td>Keterangan</td>';
					$.each(data['nilai_L_hitung'], function(key, value) {
						if(value < data['tabel_L'])
						{
							html += '<td>Normal</td>';
						} else{
							html += '<td>Tidak Normal</td>';
							}
					});
					
					html += '</tr>';
					html += '<td>Nilai Tabel L</td>';
					html += '<td>'+data['tabel_L']+'</td>';
					html += '<tr>';
					html += '</tr>';
					html += '</table>';
					
					$('#normalitas').append(html);
					$('#modal_normalitas').modal('toggle');
					hide_loading();
					},'json');
					
			}
			
				
	}
}

function modal_tanda_reliabel()
{
	$('#modal_tanda_reliabel').modal('toggle');
}

function tanda_reliabel(status)
{
	$.post('<?php echo base_url(); ?>index.php/uji/tanda_reliabel/'+status, {
		id_kuesioner: $('#id_kuesioner').val()
	});
	$('#modal_tanda_reliabel').modal('hide');
	if(status == '1'){
		alert('Data ini reliabel!');
	} else {
		alert('Data ini tidak reliabel!');
	}
			
}

function modal_tanda_valid()
{
	$('#modal_tanda_valid').modal('toggle');
}

function tanda_valid(status)
{
	$.post('<?php echo base_url(); ?>index.php/uji/tanda_valid/'+status, {
		id_kuesioner: $('#id_kuesioner').val()
	});
	$('#modal_tanda_valid').modal('hide');
	if(status == '1'){
		alert('Data ini valid!');
	} else {
		alert('Data ini tidak valid!');
	}
			
}

</script>
<?php
include 'include/footer.php';
} else{ 
		echo 'Anda belum login, silahkan login terlebih dahulu dengan klik link berikut';
		echo '<a href="'.base_url('index.php/guest').'">Kembali</a>';
	}
} else
	{
		echo 'Anda belum login, silahkan login terlebih dahulu dengan klik link berikut';
		echo '<a href="'.base_url('index.php/guest').'">Kembali</a>';
	}
	
?>